﻿using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.SatinAlma.YeniYapi
{
    public partial class FoyIncele : XPopupForm
    {
        public string POLinesIds
        {
            get { return poLinesIds; }
        }

        public int POId
        {
            get { return poId; }
        }

        string SpName;
        public FoyIncele(string spName = "GetPOlinesIdsByBarkod")
        {
            InitializeComponent();
            SpName = spName;
            this.barcode_read += new barcode_readHandler(BarkodOkuEkle_barcode_read);
        }

        string barcodeBuffer = string.Empty;

        void BarkodOkuEkle_barcode_read(string barcode)
        {            
            barcodeBuffer = string.Empty;
        }

        private void FoyIncele_Load(object sender, EventArgs e)
        {
            ShowOnlyFooter();            
            btnOk.Click += btnKaydet_Click;
            this.Shown +=  BarkodOkuEkle_Shown;
            this.KeyPress += BarkodOkuEkle_KeyPress;
            this.KeyPreview = true;
            lblInfo.ForeColor = System.Drawing.Color.YellowGreen;
        }

        void BarkodOkuEkle_Shown(object sender, EventArgs e)
        {
            spinner.Focus();
        }

        delegate void barcode_readHandler(string barcode);
        event barcode_readHandler barcode_read;               
        string poLinesIds = string.Empty;
        int poId = 0;
        
        void btnKaydet_Click(object sender, EventArgs e)
        { 
            int barkod = Convert.ToInt32(spinner.Value);
            DataTable dt = ExecuteDataTable(SpName,barkod);
            if (dt.Rows.Count == 0)
            {
                lblInfo.Visible = true;
                return;
            }
            DataRow dr = dt.Rows[0];
            poLinesIds = dr["PolinesIds"].ToStr();
            poId = dr["PoId"].ToInt(-1);
            this.DialogResult =  DialogResult.OK;
        }
         
        private void BarkodOkuEkle_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnOk.PerformClick();
            }

            //bool isDigit = Char.IsDigit(e.KeyChar);
            //if (isDigit)
            //{
            //    memoEdit1.Text += e.KeyChar.ToString();
            //    if (memoEdit1.Text.Length == 12)
            //    {
            //        barcode_read(barcodeBuffer);
            //        memoEdit1.SelectionStart = memoEdit1.Text.Length;
            //        btnKaydet.PerformClick();

            //    } 
                
            //}
        }
    }
}